package com.easy.linkerbuilder;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mBtnLast;
    private Button mBtnNext;
    private TextView mTvWarning;
    private EditText mEtValue;
    private List<ActionModel> actionModels = new ArrayList<>();
    private ActionModel actionModel;
    private static BaseRuleModel ruleModel = new BaseRuleModel();
    private int position = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnLast = findViewById(R.id.btn_last);
        mBtnNext = findViewById(R.id.btn_next);
        mTvWarning = findViewById(R.id.tv_warning);
        mEtValue = findViewById(R.id.et_value);
        mBtnLast.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
        initAction();

        //**直接在代码中生成linker，这里只做demo示范。
        Log.e("Linker", ModelBuilder.buildZuiDaModel().toBase64Linker());
        //**生成linker后如何自定义订阅源请看assets下的machine.json格式
    }

    private void initAction() {
        actionModels.add(new ActionModel(0, "请输入引擎名称", false));
        actionModels.add(new ActionModel(1, "请输入引擎版本", false));
        actionModels.add(new ActionModel(2, "请输入请求编码 不填默认为utf-8", true));
        actionModels.add(new ActionModel(3, "请输入返回编码 不填默认为utf-8", true));
        actionModels.add(new ActionModel(4, "请输入浏览器UA", true));
        actionModels.add(new ActionModel(5, "请输入域名来源 例如：http://www.baidu.com", false));
        actionModels.add(new ActionModel(6, "请输入搜索链接 例如：http://www.xxx.com/search.php?keyWords=%s&page=%s", false));
        actionModels.add(new ActionModel(7, "请输入搜索结果规则 <整个匹配>", false));
        actionModels.add(new ActionModel(8, "请输入详情页链接前缀", true));
        actionModels.add(new ActionModel(9, "请输入搜索结果详情页链接规则 <子匹配>", false));
        actionModels.add(new ActionModel(10, "请输入搜索结果标题规则 <子匹配>", false));
        actionModels.add(new ActionModel(11, "请输入类型标签规则 <子匹配>", true));
        actionModels.add(new ActionModel(12, "请输入时间标签规则 <子匹配>", true));

        actionModels.add(new ActionModel(13, "请输入详情页图片前缀", true));
        actionModels.add(new ActionModel(14, "请输入详情页图片规则 <子匹配>", true));
        actionModels.add(new ActionModel(15, "请输入详情页简介<子匹配>", true));
        actionModels.add(new ActionModel(16, "请输入播放地址列表规则 <整个匹配>", false));
        actionModels.add(new ActionModel(17, "请输入分享链接列表规则 <整个匹配>", true));
        actionModels.add(new ActionModel(18, "请输入下载链接列表规则 <整个匹配>", true));
        actionModels.add(new ActionModel(19, "请输入播放链接&播放标题规则 <整个匹配>", false));
        actionModels.add(new ActionModel(20, "请输入播放链接规则 <子匹配>", false));
        actionModels.add(new ActionModel(21, "请输入播放标题规则 <子匹配>", false));
        actionModels.add(new ActionModel(22, "请检查生成的JSON是否符合预期", false));
        actionModels.add(new ActionModel(23, "请复制Linker前往APP进行测试", false));
        next();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_last:
                last();
                break;
            case R.id.btn_next:
                next();
                break;
        }
    }

    private void last() {
        mBtnLast.setVisibility(View.GONE);
        if (position - 1 < 0) {
            return;
        }
        position -= 1;
        actionModel = actionModels.get(position);
        mEtValue.setHint(actionModel.getWarning());
        mTvWarning.setText(String.format("%s%s", actionModel.getWarning(), actionModel.isNullable() ? "，可空" : "，不可空"));
        if (position > 0) {
            mBtnLast.setVisibility(View.VISIBLE);
        } else {
            mBtnLast.setVisibility(View.GONE);
        }
        mBtnNext.setVisibility(View.VISIBLE);
        mBtnNext.setText("下一步");
        if (position == 0) {
            mEtValue.setText(ruleModel.getName());
        } else if (position == 1) {
            mEtValue.setText(ruleModel.getVersion());
            //ruleModel.setName(mEtValue.getText().toString());
        } else if (position == 2) {
            mEtValue.setText(ruleModel.getReqCharset());
            // ruleModel.setVersion(mEtValue.getText().toString());
        } else if (position == 3) {
            mEtValue.setText(ruleModel.getHtmlCharset());
            // ruleModel.setReqCharset(mEtValue.getText().toString());
        } else if (position == 4) {
            mEtValue.setText(ruleModel.getUserAgent());
            // ruleModel.setHtmlCharset(mEtValue.getText().toString());
        } else if (position == 5) {
            mEtValue.setText(ruleModel.getBaseUrl());
            // ruleModel.setUserAgent(mEtValue.getText().toString());
        } else if (position == 6) {
            mEtValue.setText(ruleModel.getSearchUrl());
            // ruleModel.setBaseUrl(mEtValue.getText().toString());
        } else if (position == 7) {
            mEtValue.setText(ruleModel.getRuleResultList());
            // ruleModel.setSearchUrl(mEtValue.getText().toString());
        } else if (position == 8) {
            mEtValue.setText(ruleModel.getRuleResultLinkHeader());
            //  ruleModel.setRuleResultList(mEtValue.getText().toString());
        } else if (position == 9) {
            mEtValue.setText(ruleModel.getRuleResultLink());
            //   ruleModel.setRuleResultLinkHeader(mEtValue.getText().toString());
        } else if (position == 10) {
            mEtValue.setText(ruleModel.getRuleResultTitle());
            // ruleModel.setRuleResultLink(mEtValue.getText().toString());
        } else if (position == 11) {
            mEtValue.setText(ruleModel.getRuleResultType());
            // ruleModel.setRuleResultTitle(mEtValue.getText().toString());
        } else if (position == 12) {
            mEtValue.setText(ruleModel.getRuleResultTime());
            // ruleModel.setRuleResultType(mEtValue.getText().toString());
        } else if (position == 13) {
            mEtValue.setText(ruleModel.getRuleDetailCoverHeader());
            // ruleModel.setRuleResultTime(mEtValue.getText().toString());
        } else if (position == 14) {
            mEtValue.setText(ruleModel.getRuleDetailCover());
            // ruleModel.setRuleDetailCoverHeader(mEtValue.getText().toString());
        } else if (position == 15) {
            mEtValue.setText(ruleModel.getRuleDetailDesc());
            // ruleModel.setRuleDetailCover(mEtValue.getText().toString());
        } else if (position == 16) {
            mEtValue.setText(ruleModel.getRuleDetailListM3u8());
            // ruleModel.setRuleDetailDesc(mEtValue.getText().toString());
        } else if (position == 17) {
            mEtValue.setText(ruleModel.getRuleDetailListShare());
            // ruleModel.setRuleDetailListM3u8(mEtValue.getText().toString());
        } else if (position == 18) {
            mEtValue.setText(ruleModel.getRuleDetailDownList());
            // ruleModel.setRuleDetailListShare(mEtValue.getText().toString());
        } else if (position == 19) {
            mEtValue.setText(ruleModel.getRuleDetailMain());
            // ruleModel.setRuleDetailDownList(mEtValue.getText().toString());
        } else if (position == 20) {
            mEtValue.setText(ruleModel.getRuleDetailLink());
            // ruleModel.setRuleDetailMain(mEtValue.getText().toString());
        } else if (position == 21) {
            mEtValue.setText(ruleModel.getRuleDetailTitle());
            // ruleModel.setRuleDetailLink(mEtValue.getText().toString());
            mBtnNext.setText("生成JSON");
        } else if (position == 22) {
            mEtValue.setText(ruleModel.toJsonString());
            //  ruleModel.setRuleDetailTitle(mEtValue.getText().toString());
            mBtnNext.setText("生成Linker");
        } else {
            mBtnNext.setVisibility(View.GONE);
            mEtValue.setText(ruleModel.toBase64Linker());
            new AlertDialog.Builder(this)
                    .setTitle("已生成Linker")
                    .setMessage(ruleModel.toBase64Linker())
                    .setPositiveButton("复制Linker", (dialog, which) -> {
                        copyText(this, ruleModel.toBase64Linker());
                        Toast.makeText(this, "已复制Linker", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", (dialog, which) -> {

                    })
                    .show();
        }

    }

    private void next() {
        if (position + 1 > 23) {
            return;
        }
        position += 1;
        actionModel = actionModels.get(position);
        mEtValue.setHint(actionModel.getWarning());
        mTvWarning.setText(String.format("%s%s", actionModel.getWarning(), actionModel.isNullable() ? "，可空" : "，不可空"));
        if (position > 0) {
            mBtnLast.setVisibility(View.VISIBLE);
        } else {
            mBtnLast.setVisibility(View.GONE);
        }
        mBtnNext.setVisibility(View.VISIBLE);
        mBtnNext.setText("下一步");
        if (position == 0) {
            mEtValue.setText(ruleModel.getName());
        } else if (position == 1) {
            ruleModel.setName(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getVersion());
        } else if (position == 2) {
            ruleModel.setVersion(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getReqCharset());
        } else if (position == 3) {
            ruleModel.setReqCharset(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getHtmlCharset());
        } else if (position == 4) {
            ruleModel.setHtmlCharset(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getUserAgent());
        } else if (position == 5) {
            ruleModel.setUserAgent(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getBaseUrl());
        } else if (position == 6) {
            ruleModel.setBaseUrl(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getSearchUrl());
        } else if (position == 7) {
            ruleModel.setSearchUrl(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultList());
        } else if (position == 8) {
            ruleModel.setRuleResultList(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultLinkHeader());
        } else if (position == 9) {
            ruleModel.setRuleResultLinkHeader(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultLink());
        } else if (position == 10) {
            ruleModel.setRuleResultLink(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultTitle());
        } else if (position == 11) {
            ruleModel.setRuleResultTitle(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultType());
        } else if (position == 12) {
            ruleModel.setRuleResultType(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleResultTime());
        } else if (position == 13) {
            ruleModel.setRuleResultTime(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailCoverHeader());
        } else if (position == 14) {
            ruleModel.setRuleDetailCoverHeader(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailCover());
        } else if (position == 15) {
            ruleModel.setRuleDetailCover(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailDesc());
        } else if (position == 16) {
            ruleModel.setRuleDetailDesc(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailListM3u8());
        } else if (position == 17) {
            ruleModel.setRuleDetailListM3u8(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailListShare());
        } else if (position == 18) {
            ruleModel.setRuleDetailListShare(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailDownList());
        } else if (position == 19) {
            ruleModel.setRuleDetailDownList(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailMain());
        } else if (position == 20) {
            ruleModel.setRuleDetailMain(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailLink());
        } else if (position == 21) {
            ruleModel.setRuleDetailLink(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.getRuleDetailTitle());
            mBtnNext.setText("生成JSON");
        } else if (position == 22) {
            ruleModel.setRuleDetailTitle(mEtValue.getText().toString());
            mEtValue.setText(ruleModel.toJsonString());
            mBtnNext.setText("生成Linker");
        } else {
            mBtnNext.setVisibility(View.GONE);
            mEtValue.setText(ruleModel.toBase64Linker());
            new AlertDialog.Builder(this)
                    .setTitle("已生成Linker")
                    .setMessage(ruleModel.toBase64Linker())
                    .setPositiveButton("复制Linker", (dialog, which) -> {
                        copyText(this, ruleModel.toBase64Linker());
                        Toast.makeText(this, "已复制Linker", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", (dialog, which) -> {

                    })
                    .show();
        }
    }

    public static void copyText(Context context, String text) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        assert clipboardManager != null;
        clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
    }
}
